package br.com.vacine.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.vacine.Entity.UsuarioEntity;
import br.com.vacine.Entity.VacinaEntity;
import br.com.vacine.Entity.VacinacaoEntity;

public class DB {
	
	private SQLiteDatabase db;
	
	public DB(Context contex){
		DBsqLite auxDb = new DBsqLite(contex);
		db = auxDb.getWritableDatabase();
	}


	public void insert(Object obj, int num){
		ContentValues valores = new ContentValues();

		
		if (num == 1) {
				VacinaEntity vacina = (VacinaEntity) obj;
				valores.put("nome", vacina.getNomeVacina());
				valores.put("descricao", vacina.getDescricao());
//				valores.put("idade", vacina.getIdade());
				
				db.insert("vacina", null, valores);
			}
		
		if (num == 2) {
			UsuarioEntity usuario = (UsuarioEntity) obj;
			valores.put("nome", usuario.getNome());
			valores.put("email", usuario.getEmail());
			valores.put("dataNascimento", usuario.getDataNascimento());
			valores.put("sexo", usuario.getSexo());
			
			db.insert("usuario", null, valores);
		}
		
		if(num == 3){
			VacinacaoEntity vacinacao = (VacinacaoEntity) obj;
			valores.put("idVacina", vacinacao.getIdVacina());
			valores.put("idUsuario", vacinacao.getIdUsuario());
			valores.put("data", vacinacao.getData());
		
			db.insert("vacinacao", null, valores);
		}
		
	}
	
	public void update(Object obj, int num){
		ContentValues valores = new ContentValues();
		
		if (num == 1) {
			
			VacinaEntity vacina = (VacinaEntity) obj;
			valores.put("nome", vacina.getNomeVacina());
			valores.put("descricao", vacina.getDescricao());
			valores.put("idade", vacina.getIdade());
			
			db.update("usuario", valores, "idVacina = ?", new String[]{""+vacina.getIdVacina()});
		}
		
		
		if (num == 2) {
			
			UsuarioEntity usuario = (UsuarioEntity) obj;
			valores.put("nome", usuario.getNome());
			valores.put("email", usuario.getEmail());
			valores.put("dataNascimento", usuario.getDataNascimento().toString());
			valores.put("sexo", usuario.getSexo());
			
			db.update("usuario", valores, "idUsuario = ?", new String[]{""+usuario.getIdUsuario()});
		}
	}
	
	public void delete(int id, int num){
		
		if (num == 1) {
			
			VacinaEntity vacina = new VacinaEntity();			
			db.delete("vacina", "idVacina = "+vacina.getIdVacina(), null);
		}
		
		if (num == 2) {

			db.delete("usuario", "idUsuario = "+ id, null);
		}
	}
	
	public void delVacinaHistorico(int id){
			
			db.delete("vacinacao", "idVacinacao = "+id, null);
	}

	public List<UsuarioEntity> listUsuario(){

		List<UsuarioEntity> list = new ArrayList<UsuarioEntity>();
		
		String[] colunas = new String[]{"idUsuario", "nome", "email", "dataNascimento", "sexo"};
		
		Cursor cursor = db.query("usuario", colunas,/**WEHRE */ null,/** Subistitui ?*/ null, /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
					UsuarioEntity user = new UsuarioEntity();
					user.setIdUsuario(cursor.getInt(0));
					user.setNome(cursor.getString(1));
					user.setEmail(cursor.getString(2));
					user.setDataNascimento(cursor.getString(3));
					user.setSexo(cursor.getString(4));
					list.add(user);
		
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}

	public List<UsuarioEntity> filtroUsuario(int where){

		List<UsuarioEntity> list = new ArrayList<UsuarioEntity>();
		
		String[] colunas = new String[]{"idUsuario", "nome", "email", "dataNascimento", "sexo"};
		
		Cursor cursor = db.query("usuario", colunas,"idUsuario = "+where,/** Subistitui ?*/ null, /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
					UsuarioEntity user = new UsuarioEntity();
					user.setIdUsuario(cursor.getInt(0));
					user.setNome(cursor.getString(1));
					user.setEmail(cursor.getString(2));
					user.setDataNascimento(cursor.getString(3));
					user.setSexo(cursor.getString(4));
					list.add(user);
		
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}
	
	public boolean pesquisaUsuario(String where){

		String[] colunas = new String[]{"idUsuario", "nome", "email", "dataNascimento", "sexo"};
		
		Cursor cursor = db.query("usuario", colunas,"nome = '"+where +"';",/** Subistitui ?*/ null, /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			
			return true;
		}else

			return false;
		
	}
	
	public List<VacinaEntity> listVacinaHist(int where){
	
		List<VacinaEntity> list = new ArrayList<VacinaEntity>();
		String[] colunas = new String[]{"idVacina", "nome", "descricao", "idade"};
		
		
		Cursor cursor = db.query("vacina", colunas,"idVacina = "+where,/** Subistitui ?*/ null, /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
		
				VacinaEntity vacina = new VacinaEntity();
				vacina.setIdVacina(cursor.getInt(0));
				vacina.setNomeVacina(cursor.getString(1));
				vacina.setDescricao(cursor.getString(2));
				vacina.setIdade(cursor.getFloat(3));
				list.add(vacina);
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}
	
	
	public List<VacinaEntity> listVacina(){
		
		List<VacinaEntity> list = new ArrayList<VacinaEntity>();
		String[] colunas = new String[]{"idVacina", "nome", "descricao", "idade"};
		
		Cursor cursor = db.query("vacina", colunas,/**WEHRE */ null,/** Subistitui ?*/ null, /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
		
				VacinaEntity vacina = new VacinaEntity();
				vacina.setIdVacina(cursor.getInt(0));
				vacina.setNomeVacina(cursor.getString(1));
				vacina.setDescricao(cursor.getString(2));
				vacina.setIdade(cursor.getFloat(3));
				list.add(vacina);
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}
	
	public List<VacinaEntity> listVacinaInformativos() {

		List<VacinaEntity> list = new ArrayList<VacinaEntity>();
		String[] colunas = new String[] { "idVacina", "nome", "descricao",
				"idade" };

		Cursor cursor = db.query("vacina", colunas, /** WEHRE */null, /** Subistitui ? */null, /** groupBy */null, /** having */null, "nome ASC");

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();

			do {

				VacinaEntity vacina = new VacinaEntity();
				vacina.setIdVacina(cursor.getInt(0));
				vacina.setNomeVacina(cursor.getString(1));
				vacina.setDescricao(cursor.getString(2));
				vacina.setIdade(cursor.getFloat(3));
				list.add(vacina);

			} while (cursor.moveToNext());
		}
		cursor.close();

		return (list);

	}
	
	
	public List<VacinacaoEntity> listHistorico(int where){
		
		List<VacinacaoEntity> list = new ArrayList<VacinacaoEntity>();
		String[] colunas = new String[]{"idVacinacao", "idVacina", "idUsuario", "data"};

		Cursor cursor = db.query("vacinacao", colunas, "idUsuario = "+ where ,null , /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
		
				VacinacaoEntity vacinacao = new VacinacaoEntity();
				
				vacinacao.setIdVacinacao(cursor.getInt(0));
				vacinacao.setIdVacina(cursor.getInt(1));
				vacinacao.setIdUsuario(cursor.getInt(2));
				vacinacao.setData(cursor.getString(3));
				
				list.add(vacinacao);
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}
	
	public List<VacinacaoEntity> listaFiltroExcluir(int idVacina, int idUsuario, String data){
		
		List<VacinacaoEntity> list = new ArrayList<VacinacaoEntity>();
		String[] colunas = new String[]{"idVacinacao", "idVacina", "idUsuario", "data"};

		Cursor cursor = db.query("vacinacao", colunas, "(idVacina = "+idVacina+" and idUsuario = "+idUsuario+ " and data = '"+data+"');"
				,null , /**groupBy */null,/**having*/ null, null);
		
		if(cursor.getCount() > 0){
			cursor.moveToFirst();
			
			do{
		
				VacinacaoEntity vacinacao = new VacinacaoEntity();
				
				vacinacao.setIdVacinacao(cursor.getInt(0));
				vacinacao.setIdVacina(cursor.getInt(1));
				vacinacao.setIdUsuario(cursor.getInt(2));
				vacinacao.setData(cursor.getString(3));
				
				list.add(vacinacao);
				
			}while(cursor.moveToNext());
		}
		cursor.close();
		
		return(list);
		
	}

}